package mapEditor.common
{
	import flash.geom.Point;
	
	import iso.IsoDisplayObject;
	import iso.IsoPoint;
	import iso.IsoUtils;
	
	import mapEditor.model.EditorModel;
	import mapEditor.view.MapRoadPoint;
	import mapEditor.vo.MapGridVO;
	
	public class RoadGrid extends IsoDisplayObject
	{
		private var _model:EditorModel;
		
		public function RoadGrid(model:EditorModel)
		{
			_model=model;
		}
		
		/**
		 * 增加路点 
		 * @param pt 坐标
		 */		
		public function addRoadPoint(pt:Point, type:int):void
		{
			var pd:IsoPoint=IsoUtils.screenToSpace(pt);
			pd.x=Math.floor(pd.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			pd.y=Math.floor(pd.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			pd.z=Math.floor(pd.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			
			pt=IsoUtils.spaceToScreen(pd);
			var roadPoint:MapRoadPoint=new MapRoadPoint(_model.mapVO.cellSize, pd);
			
			if(_model.mapVO.mapType==0)
			{
				if(!_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)] || !_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)] || _model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadType==type)
				{
					return;
				}
				_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadType=type;
				_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadPoint=roadPoint;
			}
			else
			{
				if(!_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)] || !_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)] || _model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadType==type)
				{
					return;
				}
				_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadType=type;
				_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadPoint=roadPoint;
			}
			
			addChild(roadPoint);
		}
		
		/**
		 * 清除路点 
		 * @param pt 坐标
		 */		
		public function clearRoadPoint(pt:Point):void
		{
			var pd:IsoPoint=IsoUtils.screenToSpace(pt);
			pd.x=Math.floor(pd.x/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			pd.y=Math.floor(pd.y/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			pd.z=Math.floor(pd.z/_model.mapVO.cellSize)*_model.mapVO.cellSize;
			pt=IsoUtils.spaceToScreen(pd);
			
			if(_model.mapVO.mapType==0)
			{
				if(!_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)] || !_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)] || _model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadType==0)
				{
					return;
				}
				
				_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadType=0;
				var boxPoint:MapRoadPoint=_model.mapVO.mapGridList[Math.floor(pd.x/_model.mapVO.cellSize)][Math.floor(pd.z/_model.mapVO.cellSize)].roadPoint as MapRoadPoint;
			}
			else
			{
				if(!_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)] || !_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)] || _model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadType==0)
				{
					return;
				}
				
				_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadType=0;
				boxPoint=_model.mapVO.mapGridList[Math.floor(pt.x/_model.mapVO.cellSize)][Math.floor(pt.y/_model.mapVO.cellSize)].roadPoint as MapRoadPoint;
			}
			
			if(boxPoint.parent) boxPoint.parent.removeChild(boxPoint);
			boxPoint.dispose();
			boxPoint=null;
		}
		
		/**
		 *重新更新路点 
		 */		
		public function update(roadList:Array):void
		{
			clearAll();//清理原路点数据及对象
			
			if(_model.mapVO.mapType==0)
			{
				for(var i:int=0;i<_model.mapVO.mapRow;i++)
				{
					_model.mapVO.mapGridList[i]=[];
					for(var c:int=0;c<_model.mapVO.mapCols;c++)
					{
						_model.mapVO.mapGridList[i][c]=new MapGridVO();
						_model.mapVO.mapGridList[i][c].roadType=roadList[0];
						roadList.shift();
						
						if(_model.mapVO.mapGridList[i][c].roadType==1)
						{
							var ipt:IsoPoint=new IsoPoint(i*_model.mapVO.cellSize, 0, c*_model.mapVO.cellSize);
							_model.mapVO.mapGridList[i][c].roadPoint=new MapRoadPoint(_model.mapVO.cellSize, ipt);
						}
					}
				}
			}
			else
			{
				for(i=0;i<_model.mapVO.mapCols*2;i++)
				{
					_model.mapVO.mapGridList[i]=[];
					for(c=0;c<=_model.mapVO.mapRow;c++)
					{
						_model.mapVO.mapGridList[i][c]=new MapGridVO();
						_model.mapVO.mapGridList[i][c].roadType=roadList[0];
						roadList.shift();
						
						if(_model.mapVO.mapGridList[i][c].roadType==1)
						{
							var pt:Point=new Point(i*_model.mapVO.cellSize, (c+0.5)*_model.mapVO.cellSize);
							ipt=IsoUtils.screenToSpace(pt);
							_model.mapVO.mapGridList[i][c].roadPoint=new MapRoadPoint(_model.mapVO.cellSize, ipt);
						}
					}
				}
			}
			
			
			while(numChildren>0)
			{
				var rod:MapRoadPoint=getChildAt(0) as MapRoadPoint;
				if(rod)
				{
					if( rod.parent) rod.parent.removeChild(rod);
					rod.dispose();
				}
				rod=null;
			}
			
			for(var j:int=0;j<_model.mapVO.mapGridList.length;j++)
			{
				for(var x:int=0;x<_model.mapVO.mapGridList[j].length;x++)
				{
					if(_model.mapVO.mapGridList[j][x].roadType==1)
						addChild(_model.mapVO.mapGridList[j][x].roadPoint);
				}
			}
		}
		
		/**
		 *清除所有路点及对象 
		 */		
		public function clearAll():void
		{
			while(numChildren>0)
			{
				var rod:MapRoadPoint=getChildAt(0) as MapRoadPoint;
				if(rod)
				{
					if(rod.parent) rod.parent.removeChild(rod);
					rod.dispose();
				}
				rod=null;
			}
			
			for(var i:int=0;i<_model.mapVO.mapGridList.length;i++)
			{
				for(var c:int=0;c<_model.mapVO.mapGridList[i].length;c++)
				{
					var mapGridVO:MapGridVO=_model.mapVO.mapGridList[i][c] as MapGridVO;
					if(mapGridVO) mapGridVO.dispose();
					mapGridVO=null;
				}
				_model.mapVO.mapGridList[i]=null;
			}
			_model.mapVO.mapGridList=[];
		}
		
		override public function dispose():void
		{
			super.dispose();
			clearAll();
			if(parent) parent.removeChild(this);
		}
	}
}